﻿Imports Library

Public Class StockInfoLineControl

#Region "property"


    ''' <summary>
    ''' if false display all data inside picturebox
    ''' if true display font size as set and scroll text to display
    ''' </summary>
    ''' <value> </value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property IsScroll As Boolean
        Get
            Return m_IsScroll
        End Get
        Set(value As Boolean)
            m_IsScroll = value
            If value = True Then
                tmrScroll.Start()
            Else
                tmrScroll.Stop()
            End If
        End Set
    End Property

    ''' <summary>
    ''' Show fields for display,element from 0 to field count,field 0 is preserved,
    ''' </summary>
    ''' <value>true-hide;false-display</value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ShowList As Boolean()
        Get
            Return m_ShowList
        End Get
        Set(value As Boolean())
            If value.Length = clsStockInfo.FieldNumber + 1 Then
                m_ShowList = value
                UpdateDisplay() 'force to update display at once
            End If
        End Set
    End Property


#End Region

#Region "method"

    Public Sub SetStockInfo(ByVal value As clsStockInfo)
        If value Is Nothing Then Exit Sub
        'If m_StockInfo.Compare(value) = False Then
        m_StockInfo = value
        SetPriceColor()
        UpdateDisplay()
        'End If
    End Sub
#End Region

    Private m_StockInfo As clsStockInfo
    Private m_IsScroll As Boolean 'if the data scroll display or display all
    Private m_BitMap As Bitmap 'contains the image for display
    Private m_ShowList() As Boolean 'hide fields list

    Private m_CurrentPriceColor As Color 'current price text color,red-raise;green-down;black-remain
    Private m_PreviousPrice As Double 'last price
    Private Const m_ImageWidthBuffer As Integer = 1920 'normaly the width won't exceed HD screen resolution

    Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        m_StockInfo = New clsStockInfo

        m_BitMap = New Bitmap(m_ImageWidthBuffer, picCanvas.Height)
        m_CurrentPriceColor = Color.Black
        m_PreviousPrice = -1

        'default no hidden
        ReDim m_ShowList(clsStockInfo.FieldNumber)
        For i As Integer = 0 To clsStockInfo.FieldNumber
            m_ShowList(i) = True
        Next
    End Sub

    Private Sub UpdateDisplay()
        Dim g As Graphics = Graphics.FromImage(m_BitMap)
        Dim currentPoint As New Point(0, 0)
        Const SPACE As Integer = 10
        Static StartPointOffset As Integer = 0

        'draw background
        g.Clear(Me.BackColor)

        'draw ID
        If m_ShowList(1) = True Then
            g.DrawString(m_StockInfo.StockID, Me.Font, New SolidBrush(Me.ForeColor), currentPoint)
            currentPoint.X = g.MeasureString(m_StockInfo.StockID, Me.Font).Width + SPACE
        End If

        'draw stock name
        If m_ShowList(2) = True Then
            g.DrawString(m_StockInfo.StockName, Me.Font, New SolidBrush(Me.ForeColor), currentPoint)
            currentPoint.X = currentPoint.X + g.MeasureString(m_StockInfo.StockName, Me.Font).Width + SPACE
        End If

        'draw today open price
        If m_ShowList(3) = True Then
            g.DrawString(m_StockInfo.TodayOpen, Me.Font, New SolidBrush(Me.ForeColor), currentPoint)
            currentPoint.X = currentPoint.X + g.MeasureString(m_StockInfo.TodayOpen, Me.Font).Width + SPACE
        End If

        'draw yestoday close price
        If m_ShowList(4) = True Then
            g.DrawString(m_StockInfo.YestodayClose, Me.Font, New SolidBrush(Me.ForeColor), currentPoint)
            currentPoint.X = currentPoint.X + g.MeasureString(m_StockInfo.YestodayClose, Me.Font).Width + SPACE
        End If

        'draw current price
        If m_ShowList(5) = True Then
            g.DrawString(m_StockInfo.CurrentPrice, Me.Font, New SolidBrush(m_CurrentPriceColor), currentPoint)
            currentPoint.X = currentPoint.X + g.MeasureString(m_StockInfo.CurrentPrice, Me.Font).Width + SPACE
        End If

        'draw today Max price
        If m_ShowList(6) = True Then
            g.DrawString(m_StockInfo.TodayMax, Me.Font, New SolidBrush(Me.ForeColor), currentPoint)
            currentPoint.X = currentPoint.X + g.MeasureString(m_StockInfo.TodayMax, Me.Font).Width + SPACE
        End If

        'draw today Min price
        If m_ShowList(7) = True Then
            g.DrawString(m_StockInfo.TodayMin, Me.Font, New SolidBrush(Me.ForeColor), currentPoint)
            currentPoint.X = currentPoint.X + g.MeasureString(m_StockInfo.TodayMin, Me.Font).Width + SPACE
        End If

        'draw Date
        If m_ShowList(8) = True Then
            g.DrawString(m_StockInfo.RefreshTime.ToLongTimeString, Me.Font, New SolidBrush(Me.ForeColor), currentPoint)
            currentPoint.X = currentPoint.X + g.MeasureString(m_StockInfo.RefreshTime.ToLongTimeString, Me.Font).Width + SPACE
        End If

        If m_IsScroll = False Then
            'display full image
            picCanvas.Image = m_BitMap
            Exit Sub
        End If

        'scroll image

        'if display text is smaller than display area then do not scroll
        If currentPoint.X < picCanvas.Width Then
            'display full image
            picCanvas.Image = m_BitMap
            Exit Sub
        End If

        Dim img_Tmp As New Bitmap(m_BitMap.Width, m_BitMap.Height)
        Dim g_tmp As Graphics = Graphics.FromImage(img_Tmp)
        g_tmp.DrawImage(m_BitMap, New Point(StartPointOffset, 0))

        StartPointOffset = StartPointOffset - 1

        'the tail is with the picturebox,then draw the image head after the tail, until the first image totaly out of display area
        If (StartPointOffset + currentPoint.X) < picCanvas.Width Then
            g_tmp.DrawImage(m_BitMap, New Point(currentPoint.X + StartPointOffset, 0))
        End If

        'when the second image is reach the head of picturebox then set the first image as initial position to start over again
        If (StartPointOffset + currentPoint.X) <= 0 Then
            StartPointOffset = 0
        End If

        picCanvas.Image = img_Tmp

    End Sub


    Private Sub picCanvas_Resize(sender As System.Object, e As System.EventArgs) Handles picCanvas.Resize
        Try
            m_BitMap = Nothing
            m_BitMap = New Bitmap(m_ImageWidthBuffer, picCanvas.Height)
        Catch ex As Exception

        End Try
    End Sub

    'set current price color
    Private Sub SetPriceColor()
        'if this is the first price then use yestodays' close price
        If m_PreviousPrice < 0 Then
            m_PreviousPrice = m_StockInfo.YestodayClose
        End If

        If m_PreviousPrice < m_StockInfo.CurrentPrice Then
            m_CurrentPriceColor = Color.Red
        ElseIf m_PreviousPrice = m_StockInfo.CurrentPrice Then
            m_CurrentPriceColor = Color.Black
        Else
            m_CurrentPriceColor = Color.Green
        End If

        m_PreviousPrice = m_StockInfo.CurrentPrice
    End Sub

    Private Sub tmrScroll_Tick(sender As System.Object, e As System.EventArgs) Handles tmrScroll.Tick
        UpdateDisplay()
    End Sub
End Class
